/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.Actions;

import controller.Forms.EntryForm;
import dao.ReleaseDAO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Release;
import model.Erro;
import model.User;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Adriano
 */
public class EntryAction extends Action {

	final static Logger logger = Logger.getLogger(EntryAction.class);
	
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            User u = (User) request.getSession().getAttribute("User");
            if (u == null) {

                request.getSession().setAttribute("ErroMSG", Erro.erroMsg[Erro.NAO_AUTENTICADO]);
                return mapping.findForward("erroAlias");

            } else {

                EntryForm entryForm = (EntryForm) form;
                Release lancamento = new Release();

                lancamento.setTxtdate(entryForm.getTxtdate());
                lancamento.setLstNatureza(entryForm.getLsttNatureza());
                lancamento.setTxtConta(entryForm.getTxtConta());
                lancamento.setTxtValor(entryForm.getTxtValor().replace(".","" ).replace(",", "."));
                lancamento.setLstHistorico(entryForm.getLstHistorico());
                lancamento.setTxtComplementoHistorico(entryForm.getTxtComplementoHistorico());

                ReleaseDAO entryDAO = new ReleaseDAO();

                entryDAO.create(lancamento);
                entryDAO.closeDAO();

                return mapping.findForward("homeAlias");

            }
        } catch (Exception e) {
        	
           logger.error("CADASTRAR_LANCAMENTOACTION-" + e.getMessage());
           
            return mapping.findForward("erroAlias");
        }

    }
}
